package com.gaohui.nestedrecyclerview.kotlin

import android.content.Context
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import android.util.AttributeSet
import com.gaohui.nestedrecyclerview.kotlin.adapter.MultiTypeAdapter

class CategoryView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : ChildRecyclerView(context, attrs, defStyleAttr),
    OnUserVisibleChange {

    private val mDataList = ArrayList<Any>()

    private var hasLoadData = false

    init {
        initRecyclerView()
        initLoadMore()
    }

    private fun initRecyclerView() {
        val staggeredGridLayoutManager =
            androidx.recyclerview.widget.StaggeredGridLayoutManager(
                2,
                androidx.recyclerview.widget.StaggeredGridLayoutManager.VERTICAL
            )
        layoutManager = staggeredGridLayoutManager
        adapter = MultiTypeAdapter(mDataList)
    }

    private fun initLoadMore() {
        addOnScrollListener(object : OnScrollListener() {
            override fun onScrollStateChanged(recyclerView: androidx.recyclerview.widget.RecyclerView, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)
                tryLoadMoreIfNeed()
            }
        })

    }

    private fun tryLoadMoreIfNeed() {
        if (adapter == null) return
        val layoutManager = layoutManager
        val last: IntArray
        if (layoutManager is androidx.recyclerview.widget.StaggeredGridLayoutManager) {
            last = IntArray(layoutManager.spanCount)
            layoutManager.findLastVisibleItemPositions(last)
            for (i in last.indices) {
                if ((last[i] >= adapter!!.itemCount - 4)) {
                    if (loadMore()) return
                    break
                }
            }
        }
    }

    private fun initData() {
        hasLoadData = true
        for (i in 0..100) {
            mDataList.add("default child item $i")
        }
        adapter?.notifyDataSetChanged()
    }

    private fun loadMore(): Boolean {
        val loadMoreSize = 200
        for (i in 0..loadMoreSize) {
            mDataList.add("load more child item $i")
        }
        adapter?.notifyItemRangeChanged(mDataList.size - loadMoreSize, mDataList.size)
        return true
    }

    override fun onUserVisibleChange(isVisibleToUser: Boolean) {
        if (hasLoadData.not() && isVisibleToUser) {
            initData()
        }
    }

}